<?php

class User_Form_LoginMobileForm extends Ac_Form
{
    /**
     *
     * @var Model_User empty user with properly setted behavior
     */
    private $_user;

    protected $elementButtonDecorators = array(
        'ViewHelper',
        'Errors',
        //array(array('row' => 'HtmlTag'), array('tag' => 'button'))
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        //array(array('row' => 'HtmlTag'), array('tag' => 'p', 'class'=>'button')),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    function setUser($user)
    {
        $this->_user = $user;
    }

    protected function logIn()
    {
        $form = $this->getNewSubForm();

        /* Credential Error */
        $form->addElement('hidden', 'phone', array(
            'filters'     => array('StringTrim', 'StringToLower'),
            'label'       => '',
            'id'          => 'phone',
            'name'        => 'phone',
            'description' => 'only numbers (Optional)',
            'validators'  => array(
                'Int',
                array('StringLength', false, array(3, 20)),
            ),
            'decorators' => array(
                'ViewHelper',
                'Errors',
                //array('label', array('escape' => false, 'placement' => 'PREPEND', 'tag' => 'div', 'id' => 'error_login', 'style' => 'display:none')),
                array('label', array('escape' => false, 'placement' => 'PREPEND')),
                array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'desc errors')),
            ),
            'required'   => false,
        ));

        /* Email */
        $form->addElement('text', 'email', array(
            'id'         => 'email',
            'name'       => 'email',
            //'filters'    => array('StringTrim'),
            'validators' => array(
                'Alnum',
                array('StringLength', false, array(4, 12)),
            ),
            'decorators' => array(
                            'ViewHelper',
                            'Errors',                            
                            array('Label', array('placement' => 'prepend')),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'text')),
            ),
            'required'   => false,
            'label'      => 'Email Address',
        ));

        /* Password */
        $form->addElement('password', 'password', array(
            'id'      => 'password',
            'name'      => 'password',
            //'filters'    => array('StringTrim'),
            //'description' => '<a href="forget-password">Forgot Password?</a> </p>',
            'validators' => array(
                array('StringLength', false, array(6, 20)),
            ),
            'decorators' => array(
                            'ViewHelper',
                            'Errors',
                            array('Description', array('escape' => false, 'placement' => 'APPEND', 'tag' => 'p', 'class' => '')),
                            array('Label', array('placement' => 'prepend')),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'text')),
            ),
            'required'   => false,
            'label'      => 'Password',
        ));

        /* Submit Button */
        $form->addElement('button', 'login', array(
            'required'    => false,
            'ignore'      => true,
            'type'        => 'submit',
            'label'       => 'Login',
            'decorators' => array(
                            'ViewHelper',
                            'Errors',
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'button em button_login')),
            ),
        ));

        /*Add an hidden input for validators */
        $form->addElement('hidden', 'type', array(
            'validators' => array(
                'Alpha',
            ),
            'value'      => 'login',
            'required'   => false,
        ));
        $form->getErrors();
        //$form->setLegend('Login to Your AsiaClassified Account');
        //$form->addDecorator('fieldset');
        $this->addSubForm($form, 'login');

        
    }
    
    /*    */
    public function init()
    {
        parent::init();
        
        $this->setMethod('post');
        $this->setAction($this->getView()->geturl('login' ,'user'));
        $this->logIn();

    }

    /**
     *
     *
     * let's login the user based on provided bahavior
     *
     */
    public function save()
    {
    	echo "--------save-------<pre>";print_r($_POST['login']);
        return $this->_user->login($_POST['login']);
    }

    private function getNewSubForm()
    {
        $form = new Ac_SubForm();
        //$form->setDecorators($this->subFormDecorators);
        return $form;
    }

    /**
     *
     * @param array $post
     */
    public function isValid($post = array())
    {   
        $test = true;        
        
        //TODO: check if email address and the password are correct
        if ((empty($post['email'])) || (empty($post['password']))) {
            if (empty($post['email'])) {
                $this->getSubForm('login')->getElement('email')->addError('This information is required');
                $test = false;
            }
            if (empty($post['password'])) {
                $this->getSubForm('login')->getElement('password')->addError('This information is required');
                $this->getSubForm('login')->getElement('email')->setValue($post['email']);
                $test = false;
            }
        } 
        else {
            $user = Model_User::one(array('em' => (string)$post['email'], 'pw' => Model_User::generatePassword($post['password'])));
            
            if (empty($user)) {
                $this->getSubForm('login')->getElement('email')->setValue($post['email']);
                $this->getSubForm('login')->getElement('phone')->setLabel('<p><em>Incorrect email or password combination.</em></p> <p>The email or password you entered is incorrect. Please try again.</p>');
                $test = false;
            }
        } 
                
        //$test = parent::isValid($post);
        
        return $test;
    }

    public function addErrors(array $messages)
    {
        return $this->getSubForm('login')->addErrors($messages);
    }
}